<?php


namespace App\Wen\Utils;

use App\Models\WxSetting;
use Illuminate\Support\Facades\Cache;

/**
 * 配置
 * Class Settings
 * @package App\Wen\Utils
 */
class Settings
{

    static $cache = [];

    public static function set($key,$value, $redis = false, $multi = false)
    {
        if($multi){
            global $__MINI_GLOBAL_TENANT_ID__;
            $key_sub_fix = $__MINI_GLOBAL_TENANT_ID__ > 0 ? '__'.$__MINI_GLOBAL_TENANT_ID__ : '';
            $key .= $key_sub_fix;
        }


        $settings = new WxSetting();

        if(is_string($value)){
            $value = trim($value);
        }
        if(_empty_($value, true)){
            if(is_array($value)){
                $value = [];
            }else{
                $value = '';
            }
        }

       if((new WxSetting())->where('key',$key)->exists()){
           if(is_array($value)){
               if(end($value) === null){
                   array_pop($value);
               }
               $settings->where('key',$key)->update(['value'=>json_encode($value)]);
           }else{
               $settings->where('key',$key)->update(['value'=>$value]);
           }
       }else{
           if(is_array($value)) {
               if(end($value) == null){
                   array_pop($value);
               }
               $settings->key = $key;
               $settings->value = json_encode($value);
               $settings->save();
           }else{
               $settings->key = $key;
               $settings->value = $value;
               $settings->save();
           }

       }

       if($redis){
           Cache::tags('st')->forget('st:'.$key);
           if(is_array($value)) {
               Cache::tags('st')->put('st:'.$key, json_encode($value));
           }else{
               Cache::tags('st')->put('st:'.$key, $value);
           }
       }

    }

    public static function device_get($mp_key, $h5_key, $android_key, $ios_app_key, $pc_key, $default = '', $multi = false ){
        global $__MINI_GLOBAL_DEVICE__, $__MINI_GLOBAL_PLATFORM__;
        if($__MINI_GLOBAL_DEVICE__ == 'mp'){
            return self::get($mp_key ,$default, $multi);
        }else if($__MINI_GLOBAL_DEVICE__ == 'h5'){
            return self::get($h5_key, $default, $multi);
        }else if($__MINI_GLOBAL_DEVICE__ == 'pc'){
            return self::get($pc_key, $default, $multi);
        }else if($__MINI_GLOBAL_DEVICE__ == 'app'){
            if($__MINI_GLOBAL_PLATFORM__ == 'android'){
                return self::get($android_key, $default, $multi);
            }else{
                return self::get($ios_app_key, $default, $multi);
            }
        }
    }


    public static function get($key, $default = '', $multi = false, $redis = true)
    {
        $old_key = null;
        if($multi){
            global $__MINI_GLOBAL_TENANT_ID__;
            $key_sub_fix = $__MINI_GLOBAL_TENANT_ID__ > 0 ? '__'.$__MINI_GLOBAL_TENANT_ID__ : '';
            $old_key = $key;
            $key .= $key_sub_fix;
        }

        $typeof = gettype($default);
        if(isset(self::$cache[$key])){
            $value = self::$cache[$key];
            if($typeof == 'array'){
                if($value){
                    if(is_array($value)){
                        return $value;
                    }else{
                        return json_decode($value, true);
                    }
                }else{
                    return [];
                }
            }else if($typeof == 'integer'){
                return (int)$value;
            }else if($typeof == 'boolean'){
                return ($value ? true : false);
            }else if($typeof == 'float'){
                return (float)$value;
            }else if($typeof == 'double'){
                return (double)$value;
            }
            return $value;
        }

        if($redis){
            try{
                $value = Cache::tags('st')->get('st:'.$key);
                if($value){
                    if($typeof == 'array'){
                        if($value){
                            if(is_array($value)){
                                return $value;
                            }else{
                                return json_decode($value, true);
                            }
                        }else{
                            return [];
                        }
                    }else if($typeof == 'integer'){
                        return (int)$value;
                    }else if($typeof == 'float'){
                        return (float)$value;
                    }else if($typeof == 'double'){
                        return (double)$value;
                    }else if($typeof == 'boolean'){
                        return ($value ? true : false);
                    }
                    return $value;
                }
            }catch (\Exception $e){
                _logger_(__file__, __line__, $key);
            }
        }

        try {
            $value = WxSetting::where('key',$key)->value('value');
            if($value !== null){

            }else{
                if($multi && $old_key != $key){
                    $value = WxSetting::where('key',$old_key)->value('value');
                    if($value !== null){

                    }else{
                        $value = $default;
                    }
                }else{
                    $value = $default;
                }
            }
        }catch (\Exception $e){
            exit('<h1>Mysql数据库服务不可用或链接不上，宝塔-软件列表-mysql 重启一下看看</h1>');
        }

        self::$cache[$key] = &$value;
        if($typeof == 'array'){
            if($value){
                if(is_array($value)){
                    return $value;
                }else{
                    return json_decode($value, true);
                }
            }else{
                return [];
            }
        }else if($typeof == 'integer'){
            return (int)$value;
        }else if($typeof == 'float'){
            return (float)$value;
        }else if($typeof == 'double'){
            return (double)$value;
        }else if($typeof == 'boolean'){
            return ($value ? true : false);
        }
        return $value;
    }

}
